#include "../source/server.hpp"

// class EchoServer
// {
// public:
//       EchoServer(int port):server_(port)
//       {
//             server_.SetThreadCount(3);
//             server_.EnableInactiveRelease(10);
//             server_.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
//             server_.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
//             server_.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
//       }
//       void OnConnected(const PtrConnection& conn)
//       {
//             DBG_LOG("NEW CONNECTION:%p",conn.get());
//       }
//       void OnClosed(const PtrConnection& conn)
//       {
//             DBG_LOG("CONNECTION CLOSED:%p",conn.get());
//       }
//       void OnMessage(const PtrConnection& conn,Buffer* buf)
//       {
//             conn->Send(buf->ReaderPosition(),buf->ReadAbleSize());
//             //conn->Send(buf->ReaderPosition(),buf->ReadAbleSize());
//             buf->MoveReadOffset(buf->ReadAbleSize());
//             conn->Shutdown();
//       }
//       void Start()
//       {
//             server_.Start();
//       }
// private:
//       TcpServer server_;
// };


class EchoServer {
    private:
        TcpServer _server;
    private:
        void OnConnected(const PtrConnection &conn) {
            DBG_LOG("NEW CONNECTION:%p", conn.get());
        }
        void OnClosed(const PtrConnection &conn) {
            DBG_LOG("CLOSE CONNECTION:%p", conn.get());
        }
        void OnMessage(const PtrConnection &conn, Buffer *buf) {
            conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
            buf->MoveReadOffset(buf->ReadAbleSize());
            conn->Shutdown();
        }
    public:
        EchoServer(int port):_server(port) {
            _server.SetThreadCount(2);
            _server.EnableInactiveRelease(10);
            _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
            _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
            _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        }
        void Start() { _server.Start(); }
};

int main()
{
      EchoServer server(9999);
      server.Start();
      return 0;
}